﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MINERP.COM;

namespace MINERP.BLL.GoodsDelivery
{
    public class GoodsReturnBLL
    {
        private DAL.GoodsDelivery.GoodsReturnDAL dal = new DAL.GoodsDelivery.GoodsReturnDAL();

        #region 地区
        /// <summary>
        /// 地区
        /// </summary>
        /// <returns></returns>
        public List<string> GetAreas()
        {
            try
            {
                return dal.GetAreas();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 新增退件包记录
        /// <summary>
        /// 新增退件包记录
        /// </summary>
        public void InsertReturnPackage(string area, int stationId, int stationGoodsId,int userId, int status)
        {
            try
            {
                dal.InsertReturnPackage(area, stationId, stationGoodsId, userId, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件包
        /// <summary>
        /// 修改退件包
        /// </summary>
        public void UpdateReturnPackage(string area, int stationId, int stationGoodsId, int userId, int returnId)
        {
            try
            {
                dal.UpdateReturnPackage(area, stationId, stationGoodsId, userId, returnId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除退件包
        /// <summary>
        /// 删除退件包
        /// </summary>
        /// <param name="returnId"></param>
        public void DelReturnPackage(int returnId)
        {
            try
            {
                dal.DelReturnPackage(returnId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 退件包(按状态)
        /// <summary>
        /// 退件包(按状态)
        /// </summary>
        /// <returns></returns>
        public DataTable GetReturnPackage(int status)
        {
            try
            {
                return dal.GetReturnPackage(status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取已取件的退件包
        /// <summary>
        /// 获取已取件的退件包
        /// </summary>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public DataTable GetReturnPackageFetched(int status, DateTime start, DateTime end)
        {
            try
            {
                return dal.GetReturnPackageFetched(status, start, end);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 验证产品
        /// <summary>
        /// 验证产品
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <returns></returns>
        public OrderInfoConfirm ConfirmOrderInfo(int orderProductId)
        {
            try
            {
                DataTable dt = dal.ConfirmOrderInfo(orderProductId);
                OrderInfoConfirm info = new OrderInfoConfirm();
                if (dt == null || dt.Rows.Count == 0)
                {
                    info.OrderProductId = 0;                        //0表示产品不存在
                }
                else
                {
                    info.OrderProductId = Convert.ToInt32(dt.Rows[0]["OrderProductId"]);
                    info.ShopName = dt.Rows[0]["ShopName"].ToString();
                    info.CtmName = dt.Rows[0]["CtmName"].ToString();
                    info.OrderProductName = dt.Rows[0]["OrderProductName"].ToString();
                }
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 新增退件产品记录
        /// <summary>
        /// 新增退件产品记录
        /// </summary>
        /// <param name="returnId"></param>
        /// <param name="orderProductId"></param>
        /// <param name="fetchUserId"></param>
        /// <param name="returnReason"></param>
        public void InsertReturnOrderInfo(int returnId, int orderProductId, int fetchUserId, string returnReason)
        {
            try
            {
                dal.InsertReturnOrderInfo(returnId, orderProductId, fetchUserId, returnReason);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region　获取退件包内的产品Id集合
        /// <summary>
        /// 获取退件包内的产品Id集合
        /// </summary>
        /// <param name="returnId"></param>
        /// <returns></returns>
        public List<int> GetOrderProductIdsInReturnPackage(int returnId)
        {
            try
            {
                DataTable dt = dal.GetOrderProductInReturnPackage(returnId);
                List<int> orderProductIds = new List<int>();
                foreach (DataRow r in dt.Rows)
                {
                    orderProductIds.Add(Convert.ToInt32(r["orderProductId"]));
                }
                return orderProductIds;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion 

         #region 获取退件产品(退件包号，产品号)
        /// <summary>
        /// 获取退件产品(退件包号，产品号)
        /// </summary>
        public ReturnOrderInfo GetReturnOrderInfo(int returnId, int orderProductId)
        {
            try
            {
                DataTable dt = dal.GetReturnOrderInfo(returnId, orderProductId);
                ReturnOrderInfo info = new ReturnOrderInfo();
                info.ShopName = dt.Rows[0]["ShopName"].ToString();
                info.CtmName = dt.Rows[0]["CtmName"].ToString();
                info.ReturnInfoId = Convert.ToInt32(dt.Rows[0]["ReturnInfoId"]);
                info.OrderProductId = Convert.ToInt32(dt.Rows[0]["OrderProductId"]);
                info.OrderProductName = dt.Rows[0]["OrderProductName"].ToString();
                info.FetchUserId = Convert.ToInt16(dt.Rows[0]["FetchUserId"]);
                info.ReturnReason = dt.Rows[0]["ReturnReason"].ToString();
                return info;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 获取退件包内的所有退件产品
        /// <summary>
        /// 获取退件包内的所有退件产品
        /// </summary>
        /// <param name="returnId"></param>
        /// <returns></returns>
        public List<ReturnOrderInfo> GetReturnOrderInfos(int returnId)
        {
            try
            {
                DataTable dt = dal.GetReturnOrderInfos(returnId);
                List<ReturnOrderInfo> infos = new List<ReturnOrderInfo>();
                foreach (DataRow r in dt.Rows)
                {
                    ReturnOrderInfo info = new ReturnOrderInfo();
                    info.CtmName = r["CtmName"].ToString();
                    //if (r["DisposeRecord"] != DBNull.Value)
                    //{
                    //    info.DisposeRecord = r["DisposeRecord"].ToString();
                    //}
                    //if (r["DisposeTime"] != DBNull.Value)
                    //{
                    //    info.DisposeTime = Convert.ToDateTime(r["DisposeTime"]);
                    //}
                    //info.DisposeUserName = r["DisposeUserName"].ToString();
                    info.FetchTime = Convert.ToDateTime(r["FetchTime"]);
                    info.FetchUserName = r["FetchUserName"].ToString();
                    info.OrderProductId = Convert.ToInt32(r["OrderProductId"]);
                    info.OrderProductName = r["OrderProductName"].ToString();
                    //info.ReturnInfoId = Convert.ToInt32(r["ReturnInfoId"]);
                    info.ReturnReason = r["ReturnReason"].ToString();
                    info.ShopName = r["ShopName"].ToString();
                    info.Status = Convert.ToInt16(r["Status"].ToString());
                    ReturnOrderInfoStatus s = (ReturnOrderInfoStatus)info.Status;
                    switch (s)
                    {
                        case ReturnOrderInfoStatus.NotFetch:
                            info.StatusStr = "未取件"; break;
                        case ReturnOrderInfoStatus.Fetched:
                            info.StatusStr = "已取件"; break;
                        case ReturnOrderInfoStatus.Disposed:
                            info.StatusStr = "已处理"; break;
                    }
                    infos.Add(info);
                }
                return infos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件产品
        /// <summary>
        /// 修改退件产品
        /// </summary>
        public void ModifyReturnOrderInfo(int returnOrderInfoId, int fetchUserId, string returnReason)
        {
            try
            {
                dal.ModifyReturnOrderInfo(returnOrderInfoId, fetchUserId, returnReason);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 删除退件产品
        /// <summary>
        /// 删除退件产品
        /// </summary>
        /// <param name="returnId"></param>
        /// <param name="orderProductId"></param>
        public void DelReturnOrderInfo(int returnId, int orderProductId)
        {
            try
            {
                dal.DelReturnOrderInfo(returnId, orderProductId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件包状态未已取件
        /// <summary>
        /// 修改退件包状态未已取件
        /// </summary>
        public void ModifyReturnPackageStatusToFetch(int returnPackageStatus, int returnOrderInfoStatus, int returnId)
        {
            try
            {
                dal.ModifyReturnPackageStatusToFetch(returnPackageStatus, returnOrderInfoStatus, returnId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件包状态
        /// <summary>
        /// 修改退件包状态
        /// </summary>
        public void UpdateReturnPackageStatus(int returnId, int status)
        {
            dal.UpdateReturnPackageStatus(returnId, status);
        }
        #endregion

        #region 获取退件产品(按状态)
        /// <summary>
        /// 获取退件产品(按状态)
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public DataTable GetReturnOrderInfosByStatus(int status,DateTime start,DateTime end)
        {
            try
            {
                DataTable dt = dal.GetReturnOrderInfosByStatus(status,start,end);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 处理退件产品
        /// <summary>
        /// 处理退件产品
        /// </summary>
        public void DisposeReturnOrderInfo(int returnInfoId, int disposeUserId, string disposeRecord,int status)
        {
            try
            {
                dal.DisposeReturnOrderInfo(returnInfoId, disposeUserId, disposeRecord, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 修改退件产品状态
        /// <summary>
        /// 修改退件产品状态
        /// </summary>
        public void ModifyReturnOrderInfoStatus(int returnInfoId, int status)
        {
            try
            {
                dal.ModifyReturnOrderInfoStatus(returnInfoId, status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region 查询
        /// <summary>
        /// 查询
        /// </summary>
        public List<ReturnOrderInfo> FindReturnOrderInfo(int shopId, string ctmName, DateTime start, DateTime end)
        {
            try
            {
                DataTable dt = dal.FindReturnOrderInfo(shopId, ctmName, start, end);
                List<ReturnOrderInfo> infos = new List<ReturnOrderInfo>();
                foreach (DataRow r in dt.Rows)
                {
                    ReturnOrderInfo info = new ReturnOrderInfo();
                    info.CtmName = r["CtmName"].ToString();
                    if (r["DisposeRecord"] != DBNull.Value)
                    {
                        info.DisposeRecord = r["DisposeRecord"].ToString();
                    }
                    if (r["DisposeTime"] != DBNull.Value)
                    {
                        info.DisposeTime = Convert.ToDateTime(r["DisposeTime"]);
                    }
                    info.DisposeUserName = r["DisposeUserName"].ToString();
                    info.FetchTime = Convert.ToDateTime(r["FetchTime"]);
                    info.FetchUserName = r["FetchUserName"].ToString();
                    info.OrderProductId = Convert.ToInt32(r["OrderProductId"]);
                    info.OrderProductName = r["OrderProductName"].ToString();
                    info.ReturnInfoId = Convert.ToInt32(r["ReturnInfoId"]);
                    info.ReturnReason = r["ReturnReason"].ToString();
                    info.ShopName = r["ShopName"].ToString();
                    info.Status = Convert.ToInt16(r["Status"].ToString());
                    ReturnOrderInfoStatus s = (ReturnOrderInfoStatus)info.Status;
                    switch (s)
                    { 
                        case ReturnOrderInfoStatus.NotFetch:
                            info.StatusStr = "未取件"; break;
                        case ReturnOrderInfoStatus.Fetched:
                            info.StatusStr = "已取件"; break;
                        case ReturnOrderInfoStatus.Disposed:
                            info.StatusStr = "已处理"; break;
                    }
                    infos.Add(info);
                }
                return infos;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        //--------------数据结构------------
        /// <summary>
        /// 产品验证
        /// </summary>
        public class OrderInfoConfirm
        {
            public int OrderProductId { get; set; }
            public string ShopName { get; set; }
            public string CtmName { get; set; }
            public string OrderProductName { get; set; }
        }

        public class ReturnOrderInfo
        {
            public int ReturnInfoId { get; set; }
            public string ShopName { get; set; }
            public string CtmName { get; set; }
            public int OrderProductId { get; set; }
            public string OrderProductName { get; set; }
            public int ReturnId { get; set; }
            public int FetchUserId { get; set; }
            public string FetchUserName { get; set; }
            public DateTime FetchTime { get; set; }
            public string ReturnReason { get; set; }
            public int DisposeUserId { get; set; }
            public string DisposeUserName { get; set; }
            public string DisposeRecord { get; set; }
            public DateTime DisposeTime { get; set; }
            public int Status { get; set; }
            public string StatusStr { get; set; }
        }
    }
}
